package ScalaSpark

import org.apache.spark.{SparkConf, SparkContext}

object ScalaSortWordCount {
  def main(args: Array[String]) {
    val conf = new SparkConf //创建SparkConf对象
    conf.setAppName("ScalaSortWordCount") //设置应用程序的名称，在程序运行的监控界面可以看到名称
    conf.setMaster("local") //此时，程序在本地运行，不需要安装Spark集群

    val sc = new SparkContext(conf)
    val lines = sc.textFile("C://Users//10902//Desktop//spark.txt", 1)
    val words = lines.flatMap { line => line.split("") }
    val pairs = words.map { word => (word, 1) }
    val wordCounts = pairs.reduceByKey(_ + _)

    val countWords = wordCounts.map { wordCount => (wordCount._2, wordCount._1) }
    val sortedCountWords = countWords.sortByKey(false)
    val sortedWordCounts = sortedCountWords.map { sortedCountWord => (sortedCountWord._2, sortedCountWord._1) }

    sortedWordCounts.foreach(sortWordCount => println(sortWordCount._1 + "出现了：" + sortWordCount._2 + "次"))
  }
}
